package base.Reflct;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

public class MyReflectDemo {
    public static void main(String[] args) throws Exception {

        Student s = new Student("张三", 18, 90.0);
        savaObject(s);

    }

    public static void savaObject(Object obj) throws IllegalAccessException, IOException {
        Class clazz = obj.getClass();
        //获取所有属性
        Field[] fields = clazz.getDeclaredFields();
        BufferedWriter bw = new BufferedWriter(new FileWriter("src\\base\\反射\\d.txt"));

        for (Field field : fields) {
            field.setAccessible(true);//允许访问 private、protected 或包级私有的成员
            String name = field.getName();
            Object value = field.get(obj);

            bw.write(name + "=" + value);
            bw.newLine();
        }
        bw.close();
    }
}
